<?php

/**
 * @author 
 * @copyright 2012
 */

class Template extends FileManager
{
	private $variables 		= array();
	private $init_zone_vars = array();
	private $zone_templates = array();
	private $zone_variables = array();
	private $zone_arrays	= array();
	private $output;
	private $zones 			= array();
	private $zones_i;
	private $location;
	private $boolean_vars	= array();
	
	public function __construct()
	{
		$this->zones_i = 0;
		$this->location = 'output/cfh/';
	}
	
	public function set_vars($var, $value='', $zone=false)
	{
		if (!is_array($var))
		{
			if (!empty($value))
			{
				$this->variables[$var] = $value;
			}
			else
			{
				throw new Exception('An empty or invalid variable has been set');
			}
		}
		else
		{
			if ($zone)
			{
				$this->variables[$var] = $value;
			}
			else
			{
				foreach ($var as $key => $value)
				{
					$this->variables[$key] = $value;
				}
			}
		}
	}
	
	public function set_zone_vars($var, array $array)
	{
		// Add array
		$this->init_zone_vars[] = $array;
		$x_izv = count($this->init_zone_vars);
		$this->zone_variables[$this->zones_i.'_'.$var] = $array;
		
		// Add up the the amount of zones
		$this->zones_i++;
	}
	
	public function zone($var, array $data, $template='')
	{
		$this->zones[] = $var;			
		$this->set_zone_vars($var, $data);
		$this->zone_templates[] = $template;
	}
	
	public function set_boolean($var, $state = true)
	{
		$this->boolean_vars[$var] = $state;
	}
	
	public function load_file($file, $ajax = false)
	{
		$file = $file.'.html';
		//echo 'Bestand:'.$file;
		if (file_exists($file) && is_readable($file))
		{
			$this->content = parent::content($file);
			
			// Includes
			if (!$ajax)
			{
				$x_includes = preg_match_all('/<!-- INCLUDE (.*) -->/', $this->content, $matches);
				for ($i = 1; $i <= $x_includes; $i++)
				{
					if (preg_match('/<!-- INCLUDE (.*) -->/', $this->content, $matches))
					{
						$this->content = preg_replace('/<!-- INCLUDE (.*) -->/', parent::content($matches[1]), $this->content, 1);
					}
				}
			}
			
			// Requires (Always includes, even if the file is requested through AJAX)
			$x_requires = preg_match_all('/<!-- REQUIRE (.*) -->/', $this->content, $matches);
			for ($i = 1; $i <= $x_requires; $i++)
			{
				if (preg_match('/<!-- REQUIRE (.*) -->/', $this->content, $matches))
				{
					$this->content = preg_replace('/<!-- REQUIRE (.*) -->/', parent::content($matches[1]), $this->content, 1);
				}
			}
			
			// CSS Stylesheets
			$x_stylesheets = preg_match_all('/<!-- CSS (.*) -->/', $this->content, $matches);
			for ($i = 1; $i <= $x_stylesheets; $i++)
			{
				if (preg_match('/<!-- CSS (.*) -->/', $this->content, $matches))
				{
					$this->content = preg_replace('/<!-- CSS (.*) -->/', '<style type="text/css">'.parent::content($matches[1]).'</style>', $this->content, 1);
				}
			}
			
			// Javascript files
			$x_scripts = preg_match_all('/<!-- SCRIPT (.*) -->/', $this->content, $matches);
			for ($i = 1; $i <= $x_scripts; $i++)
			{
				if (preg_match('/<!-- SCRIPT (.*) -->/', $this->content, $matches))
				{
					$this->content = preg_replace('/<!-- SCRIPT (.*) -->/', '<script type="text/javascript">'.parent::content($matches[1]).'</script>', $this->content, 1);
				}
			}
			
			// Boolean Variables
			$find_statements_checks = preg_match_all('/<!-- IF (.*) -->(.*)<!-- ENDIF -->/', $this->content, $matches);
			// $matches[1] = first wildcard, containing the boolean variable
			// $matches[2] = second wildcard, containing the content to be shown or not
			if ($find_statements_checks >= 1)
			{
				for ($i = 1; $i <= $find_statements_checks; $i++)
				{
					$check_statement = preg_match('/<!-- IF (.*) -->(.*)<!-- ENDIF -->/s', $this->content, $matches);
					if ($check_statement)
					{
						if (in_array($matches[1], $this->boolean_vars))
						{
							$replacement = ($this->boolean_vars[$matches[1]] == true) ? $matches[2] : '';
							$this->content = preg_replace('/<!-- IF '.$matches[1].' -->'.$matches[2].'<!-- ENDIF -->/s', $replacement, $this->content, 1);
						}
					}
				}
			}
			
			// Zones
			$zone_content = '';
			
			// Initial search results
			$search_results = 0;
			
			// Loop zones
			//print_r($this->zones);
			//exit;
			foreach ($this->zones as $key => $value)
			{	
				$my = preg_match('/<!-- BEGIN '.$value.' -->(.*)<!-- END '.$value.' -->/s', $this->content, $preg_matches);
				if ($my)
				{
					$search_results++;
				}
			}
			$init_val = 'bla';
			$matches_i = 0;
			$sub_matches_i = 0;
			$zone_content = '';
			$sub_content = array();
			$new_arr_count = count(array_unique($this->zones));
			foreach(array_unique($this->zones) as $key => $value)
			{
				$zoner_content[$value] = '';
			}
			foreach ($this->zones as $key => $value)
			{
				$arr_count = count($this->zone_variables[$key.'_'.$value]);
				$i_count = 1;
				if ($arr_count > 0)
				{
					foreach ($this->zone_variables[$key.'_'.$value] as $sub_key => $sub_value)
					{
						$temp_array[$i_count.'|'.$sub_key] = $sub_value;
						if ($i_count == $arr_count)
						{
							if (preg_match('/<!-- BEGIN '.$value.' -->(.*)<!-- END '.$value.' -->/s', $this->content, $matches))
							{	
								// Assign values to zone variables
								foreach ($temp_array as $subsub_key => $subsub_value)
								{
									$sep_subsub_key = explode('|', $subsub_key);
									$matches[1] = preg_replace('/\{'.$value.'\.'.$sep_subsub_key[1].'\}/', $subsub_value, $matches[1], 1);
								}
								$zoner_content[$value] .= $matches[1];
								unset($temp_array);
							}
						}
						$i_count++;
					}
				}
				else
				{
					if (preg_match('/<!-- BEGIN '.$value.' -->(.*)<!-- END '.$value.' -->/s', $this->content, $matches))
					{
						$matches[0]	 = preg_replace('/<!-- BEGIN '.$value.' -->(.*)<!-- END '.$value.' -->/s', '', $matches[0], 1);
						$zoner_content[$value] = $matches[0];
						unset($temp_array);
					}
				}
			}
			
			if (isset($zoner_content))
			{
				if (is_array($zoner_content))
				{
					foreach ($zoner_content as $key => $value)
					{
						$this->content = preg_replace('/<!-- BEGIN '.$key.' -->(.*)<!-- END '.$key.' -->/s', $value, $this->content);
					}
				}
			}

			foreach($this->variables as $key => $value)
			{
				if (!is_array($value))
				{
					$this->content = preg_replace('/\{' . $key . '\}/', $value, $this->content);
				}
			}
		}
		else
		{
			throw new ErrorHandler('The requested HTML file ' . $file . ' could not be found');
		}
	}
	
	public function output($file, $vars, $ajax = false)
	{
		$this->set_vars($vars);
		$this->load_file($file, $ajax);
		
		// Last bit of cleaning up
		$this->content = preg_replace('/<!-- BEGIN (.*) -->/', '', $this->content);
		$this->content = preg_replace('/<!-- END (.*) -->/', '', $this->content);
		
		// Print the parsed HTML code
		echo $this->content;
		return $this->content;
	}
}

?>